$(function () {
	var vm = new Vue({
		el:'#app',
		data:{
			formData:{id:$("#id").val()},
			ifEdit:false,
			form:{},
			saveUrl:''
		},
		methods: {
			init:function(){
				var that = this;
				layui.use(['table','form','layer','tree','treeselect'], function(){
					var form = layui.form,treeselect = layui.treeselect;
					if(that.formData.id){
						//修改编辑数据
						that.ifEdit = true
						that.saveUrl = staticURL+"/basic/resourceInfoAction/edit"
						that.loadData(form,treeselect);
					}else{
						//新增数据
						that.saveUrl = staticURL+"/basic/resourceInfoAction/add"
					}
					treeselect.render({
				          elem: "#parentId",
				          data: staticURL+"/basic/resourceInfoAction/resourceTree.do",//可以是treedata，也可以是 获取treedata的URL地址
				          method: "GET",
				          click:function(data){
				        	  vm.formData.parentName=data.text;
				          }
				      });					
					//radio select vue 和layui一起用 有bug,不能自动赋值，需要设置监听，二且页面中不需要v-model
					form.on('select(status)', function(data){
						that.formData.status = data.value;
					});
					form.on('select(type)', function(data){
						that.formData.type = data.value;
					});
				});
				
			},
			goBack:function () { 
				parent.layer.close(parent.layer.getFrameIndex(window.name));
			},
			doSave:function () {
				if(!sy.validateLayuiForm($("#layui-form"))){
					return;
				}
				vm.formData.parentId=$("#parentId").val();
				$.ajax({url : vm.saveUrl,
					type : "post",
					async:false,
					dataType:'json',
					data:vm.formData,
					cache : false,
					success : function(json) {
						if (json && json.success) {
							$.when(parent._refresh()).always(vm.goBack());
						}else{
							layer.msg(json.msg);
						}
					},
					error: function(XMLHttpRequest, textStatus, errorThrown) {
						layer.msg('操作失败！');
			       }
				}); 
			},
			loadData:function(form,treeselect){
				var formData = this.formData;
				var vm  = this;
				$.ajax({url :staticURL+"/basic/resourceInfoAction/showDesc",
					data : {
						id : vm.formData.id
					},
					dataType : 'json',
					cache : false,
					success : function(json) {
						//vm.formData = json;
						sy.formLoadDataVue($(".layui-form"),json,vm.formData);
						form.render();
						treeselect.render();
					}
				});
			}
		},
		created: function () {
			this.init();
		},
		updated:function(){
			
		}
	});
});